const app = getApp();
import util from './util.js';
import authLogin from './authLogin.js'

/**
* 发送请求
* 
*/
export default function request(api, method, data, { noAuth = false, noVerify = false, isLoading = false }){
  let Url = getApp().globalData.url;
  let header = { 'content-type': 'application/x-www-form-urlencoded'};
  return new Promise((reslove, reject) => {
    if (isLoading) {
      console.info('加载中...');
      util.showLoading('加载中...');
    }
    wx.request({
      url: Url.two + api,
      method: method || 'GET',
      header: header,
      data: data || {},
      success: (res) => {
        if (res.msg == '请重新登录' || res.msg =='账号异常'){
          authLogin();
        }
        if (isLoading) {
          util.hideLoading()
        }
        reslove(res.data || '', res);
      },
      fail: (msg) => {
        if (isLoading) {
          util.hideLoading()
        }
        reject('请求失败');
      },
    })
  });
}

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
  request[method] = (api, data, opt) => request(api, method, data, opt || {})
});